package com.ruoyi.reportsnhnt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntKyqdBaDetail;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntKyqdBaDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportsnhnt.domain.bo.ReportSnhntKyqdBsInfoBo;
import com.ruoyi.reportsnhnt.domain.vo.ReportSnhntKyqdBsInfoVo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntKyqdBsInfo;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntKyqdBsInfoMapper;
import com.ruoyi.reportsnhnt.service.IReportSnhntKyqdBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 水泥混凝土抗压强度试验检测记录(立方体)Service业务层处理
 *
 * @author zxs
 * @date 2024-06-21
 */
@RequiredArgsConstructor
@Service
public class ReportSnhntKyqdBsInfoServiceImpl implements IReportSnhntKyqdBsInfoService {

    private final ReportSnhntKyqdBsInfoMapper baseMapper;
    private final ReportSnhntKyqdBaDetailMapper detailMapper;

    /**
     * 查询水泥混凝土抗压强度试验检测记录(立方体)
     */
    @Override
    @FIllBasicInfo
    public ReportSnhntKyqdBsInfoVo queryById(Long excelId) {
        ReportSnhntKyqdBsInfoVo vo = baseMapper.selectVoById(excelId);
        List<ReportSnhntKyqdBaDetail> detailList = detailMapper.selectDetailListByExcelId(excelId);
        if (CollUtil.isNotEmpty(detailList)) {
            vo.setDetailList(detailList);
        }
        return vo;
    }

    /**
     * 查询水泥混凝土抗压强度试验检测记录(立方体)列表
     */
    @Override
    public TableDataInfo<ReportSnhntKyqdBsInfoVo> queryPageList(ReportSnhntKyqdBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSnhntKyqdBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportSnhntKyqdBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询水泥混凝土抗压强度试验检测记录(立方体)列表
     */
    @Override
    public List<ReportSnhntKyqdBsInfoVo> queryList(ReportSnhntKyqdBsInfoBo bo) {
        LambdaQueryWrapper<ReportSnhntKyqdBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSnhntKyqdBsInfo> buildQueryWrapper(ReportSnhntKyqdBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSnhntKyqdBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPage() != null, ReportSnhntKyqdBsInfo::getPage, bo.getPage());
        lqw.eq(bo.getTotalPage() != null, ReportSnhntKyqdBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportSnhntKyqdBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportSnhntKyqdBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportSnhntKyqdBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportSnhntKyqdBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpxx()), ReportSnhntKyqdBsInfo::getYangpxx, bo.getYangpxx());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportSnhntKyqdBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportSnhntKyqdBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsljzt()), ReportSnhntKyqdBsInfo::getYangpsljzt, bo.getYangpsljzt());
        lqw.eq(bo.getYangplysj() != null, ReportSnhntKyqdBsInfo::getYangplysj, bo.getYangplysj());
        lqw.eq(bo.getShiyjcrq() != null, ReportSnhntKyqdBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportSnhntKyqdBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportSnhntKyqdBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportSnhntKyqdBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportSnhntKyqdBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportSnhntKyqdBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportSnhntKyqdBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getLingq()), ReportSnhntKyqdBsInfo::getLingq, bo.getLingq());
        lqw.eq(StringUtils.isNotBlank(bo.getHunntzl()), ReportSnhntKyqdBsInfo::getHunntzl, bo.getHunntzl());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportSnhntKyqdBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportSnhntKyqdBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportSnhntKyqdBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportSnhntKyqdBsInfo::getFuh, bo.getFuh());
        lqw.eq(bo.getRiq() != null, ReportSnhntKyqdBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShejqddj()), ReportSnhntKyqdBsInfo::getShejqddj, bo.getShejqddj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiaobfs()), ReportSnhntKyqdBsInfo::getJiaobfs, bo.getJiaobfs());
        lqw.eq(StringUtils.isNotBlank(bo.getShijcc()), ReportSnhntKyqdBsInfo::getShijcc, bo.getShijcc());
        lqw.eq(StringUtils.isNotBlank(bo.getYanghtj()), ReportSnhntKyqdBsInfo::getYanghtj, bo.getYanghtj());
        lqw.eq(StringUtils.isNotBlank(bo.getYonglmc()), ReportSnhntKyqdBsInfo::getYonglmc, bo.getYonglmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYonglpb()), ReportSnhntKyqdBsInfo::getYonglpb, bo.getYonglpb());
        lqw.eq(StringUtils.isNotBlank(bo.getShejtld()), ReportSnhntKyqdBsInfo::getShejtld, bo.getShejtld());
        lqw.eq(StringUtils.isNotBlank(bo.getBeiz()), ReportSnhntKyqdBsInfo::getBeiz, bo.getBeiz());
        return lqw;
    }

    /**
     * 新增水泥混凝土抗压强度试验检测记录(立方体)
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(ReportSnhntKyqdBsInfoBo bo) {
        ReportSnhntKyqdBsInfo add = BeanUtil.toBean(bo, ReportSnhntKyqdBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        //插入子表数据；
        List<ReportSnhntKyqdBaDetail> detailList = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            ReportSnhntKyqdBaDetail detail = new ReportSnhntKyqdBaDetail();
            detail.setExcelId(bo.getExcelId());
            detailList.add(detail);
        }
        detailMapper.insertBatch(detailList);
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改水泥混凝土抗压强度试验检测记录(立方体)
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(ReportSnhntKyqdBsInfoBo bo) {
        ReportSnhntKyqdBsInfo update = BeanUtil.toBean(bo, ReportSnhntKyqdBsInfo.class);
        validEntityBeforeSave(update);
        //更新子表
        List<ReportSnhntKyqdBaDetail> detailList = bo.getDetailList();
        if (detailList != null && detailList.size() != 0) {
            detailMapper.updateBatchById(detailList);
        }
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSnhntKyqdBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除水泥混凝土抗压强度试验检测记录(立方体)
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
